package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserLikeCountVo;
import com.itheima.domain.vo.UserLikeVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.service.mongo.UserLikeService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserLikeManager {

    @Reference
    private UserLikeService userLikeService;
    @Reference
    private FriendService friendService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @Reference
    private UserInfoService userInfoService;

    @Reference
    private RecommendUserService recommendUserService;
    /**
     *  互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     *  1 互相关注
     *  2 我关注
     *  3 粉丝
     *  4 谁看过我
     */
    public PageBeanVo findUserList(Integer pageNumber, Integer pageSize, Integer type) {
        //1.获得当前用户的id
        Long loginUserId = UserHolder.getId();
        //2.根据当前用户的id和当前查询用户的类型 调用方法
        PageBeanVo pageBeanVo = userLikeService.findUserList(pageNumber , pageSize , type , loginUserId);
        //UserLikeVo 此实体类需要返回id即可
        List<UserLikeVo> items = (List<UserLikeVo>) pageBeanVo.getItems();

        //3.查询用户的基本信息
        if(CollectionUtil.isNotEmpty(items)){
            for (UserLikeVo userLikeVo : items) {
                //查询用户记录
                UserInfo userInfo = userInfoService.findById(userLikeVo.getId());
                //封装数据 -> 封装基本数据 缺缘分值
                BeanUtil.copyProperties(userInfo , userLikeVo );
                //我跟返回的用户人之间的缘分值
                RecommendUser recommendUser = recommendUserService.findRecommendUser(loginUserId, userLikeVo.getId());
                if(recommendUser == null){
                    userLikeVo.setMatchRate(RandomUtil.randomInt(60,90)); // 如果没有缘分值 给默认值
                }else{
                    userLikeVo.setMatchRate(recommendUser.getScore().intValue()); //有缘分值 直接取
                }
            }
        }


        return pageBeanVo;
    }

    /*
        用户不喜欢
        */
    //用户不喜欢
    public void deleteUserLike(Long likeUserId) {
        //1. 获取登录用户id
        Long userId = UserHolder.get().getId();
        //2. 调用friendService删除好友
        friendService.delete(userId, likeUserId);
        //3. 调用环信删除好友
        huanXinTemplate.deleteContacts(userId, likeUserId);
        //4. 调用userLikeService删除我对它的喜欢
        userLikeService.delete(userId, likeUserId);
    }
}
